package com.zlp.feign;

import com.aliyun.oss.model.OSSObject;
import com.zlp.config.oss.OssTemplate;
import feign.Response;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.*;
import java.util.Objects;

@RestController
@Slf4j(topic = "FileDownFeignImpl")
@Api(value = "FileDownFeign", tags = "文件下载模块")
public class FileDownFeignImpl implements FileDownFeign {


    @Resource
    private OssTemplate ossTemplate;


    @Override
    @PostMapping(value = "/v1/download")
    public byte[] download(String fileUrl) {

        InputStream in = null;
        String localFileDir = "D:\\file\\video";
        String fileName = "20210113001.jpg";
        String filePath = localFileDir + File.separator + fileName;
        try {
            in = new FileInputStream(new File(filePath));
            byte[] bytes = new byte[in.available()];
            return bytes;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (Objects.nonNull(in)) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    @Override
    @PostMapping(value = "/v1/getObject")
    public ResponseEntity<byte[]> getObject(String downFileUrl) {

        String fileUrl = "https://greenvalley.oss-cn-shanghai.aliyuncs.com/suirenow/123456789.zip";
        downFileUrl = fileUrl.replace("https://greenvalley.oss-cn-shanghai.aliyuncs.com/", "");
        OSSObject ossObject = ossTemplate.getObject(downFileUrl);
        ResponseEntity<byte[]> entity = null;
        HttpHeaders headers = new HttpHeaders();
        BufferedInputStream bis = null;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buf = new byte[10240];
        try {
            bis = new BufferedInputStream(ossObject.getObjectContent());
            while (true) {
                int len = bis.read(buf);
                if (len < 0) {
                    break;
                }
                bos.write(buf, 0, len);
            }
            //将输出流转为字节数组，通过ResponseEntity<byte[]>返回
            byte[] b = bos.toByteArray();
            System.err.println("接收到的文件大小为：" + b.length);
            HttpStatus status = HttpStatus.OK;
            bos.close();
            entity = new ResponseEntity<byte[]>(b, headers, status);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return entity;
    }

    @Override
    /**
     * 文件（二进制数据）下载
     * @param fileType 文件类型
     * @return
     */
    @GetMapping("/downloadFile")
    public ResponseEntity<byte[]> downloadFile(String downFileUrl) {


        String fileUrl = "https://greenvalley.oss-cn-shanghai.aliyuncs.com/suirenow/123456789.zip";
        downFileUrl = fileUrl.replace("https://greenvalley.oss-cn-shanghai.aliyuncs.com/", "");
        OSSObject ossObject = ossTemplate.getObject(downFileUrl);
        ResponseEntity<byte[]> entity = null;
        HttpHeaders headers = new HttpHeaders();
        BufferedInputStream bis = null;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
//        byte[] buf = new byte[10240];
        try {
           /* bis = new BufferedInputStream(ossObject.getObjectContent());
            while (true) {
                int len = bis.read(buf);
                if (len < 0) {
                    break;
                }
                bos.write(buf, 0, len);
            }*/
            //将输出流转为字节数组，通过ResponseEntity<byte[]>返回
//            byte[] b = bos.toByteArray();
            bis = new BufferedInputStream(ossObject.getObjectContent());
            byte[] b = new byte[bis.available()];
            System.err.println("接收到的文件大小为：" + b.length);
            HttpStatus status = HttpStatus.OK;
            String fileName = "123456789.zip";
            fileName = new String(fileName.getBytes(), "iso-8859-1");
            headers.add("Content-Disposition", "attachment;filename=" + fileName);
            bos.close();
            entity = new ResponseEntity<byte[]>(b, headers, status);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return entity;
    }

    @Override
    public Response downloadFile() {

        return null;
    }


}
